/*
 * TextField.java
 * Created on July 28, 2007, 12:06 PM
 */

package view;

import javax.microedition.lcdui.Graphics;
import utils.Util;

/**
 * @author Erol Hira
 */
public class TextField extends Component {
    
    private Cursor cursor;
    
    private StringBuffer string = new StringBuffer();
    
    private boolean cursorNotified;
    
    /**
     *  simple line border around the component 
     */
    private boolean visibleLine = true;
    private Color softLineColor;
    private Color hardLineColor;
    private byte frameLineTickness = 2;
    
    
    private int maxLength = 100;
    
    
    public TextField() {
        super();        
        setupCursor();
    }
    
    public TextField(int width, int height){
        super(width, height);
        setupCursor();
    }
    
    public TextField(int x, int y, int width, int height){
        super(x, y, width, height);
        setupCursor();
    }

    /**
     *  evaluated at the end of super()
     */
    protected void init() {
        focusable = true;
        softLineColor = getFgColor();                
        hardLineColor = getFgColor();
    }

    public void paint(Graphics g) {
                
        // paint visible line
        if(visibleLine){                        
            GuiUtils.drawRectangle(g, get_x_content(), get_y_content(),
                                width, height, softLineColor, hardLineColor, 
                                frameLineTickness);
        }                
        
        cursor.paintCursor(g);
        
        drawString(g);                             
    }
    
    public void paintComponent(Graphics g){
        
        if(isPaintFlag() == false){
            return;
        }
        
        paintBorder(g);
        paintBG(g);
        paint(g);
        
        if(cursorNotified){                                 
            cursorNotified = false;            
        }                
        
        setPaintFlag(false);
    }
    
    public void setupCursor(){
        
        cursor = new Cursor(this);
        cursor.setX(get_x_content() + marginLeft);
        cursor.setY(get_y_content() + marginTop);
        cursor.start();
    }

    public void onFocus() {
        border.focus();
    }

    public void onFocusLost() {
        border.focusLost();
    }        
    
    //----------------------------------------------------------------------
    
    /**
     *  Graphics.LEFT | Graphics.VCENTER
     *  Graphics.HCENTER | Graphics.VCENTER
     */
    public void drawString(Graphics g){
        
        //g.drawString(string.toString(), get_x_content() + marginLeft, get_y_content() + marginTop, Graphics.TOP | Graphics.LEFT);
        font.write(g, string.toString(), 
                    get_x_content() + marginLeft, 
                    get_y_content() + marginTop, 
                    getWidth() - marginLeft, getHeight() - marginTop, 
                    alignment);
    }        
    
    //----------------------------------------------------------------------

    protected void doLEFT() {
        
        
    }
    
    protected void doKey(String ch){
        
        if(constraintsViolated() == false){
            
            string.append(ch);

            int strWidth = findStringWidth(string.toString());

            cursor.setX(get_x_content() + marginLeft + strWidth + 2);

            repaint();            
        }                
    }
    
    private int findStringWidth(String str){
        
        int sum = 0;        
        return font.getWidth(str);        
    }
    
    public boolean constraintsViolated(){
        
        if(findStringWidth(string.toString()) >= getUsableWidth()){
            return true;
        } else if(string.length() >= maxLength){
            return true;
        } else {
            return false;
        }
    }
    
    public int getUsableWidth(){
        
        return width - marginLeft - marginRight;
    }
    
    //----------------------------------------------------------------------

    public String getString() {
        return string.toString();
    }

    public void setString(String str) {
        string.setLength(0);
        string.append(str);
    }

    //----------------------------------------------------------------------
    
    public void setX(int x) {
        super.setX(x);
        cursor.setX(get_x_content() + marginLeft);        
    }
    
    public void setY(int y) {
        super.setY(y);
        cursor.setY(get_y_content() + marginTop);        
    }    
    
    //----------------------------------------------------------------------
    
    public boolean isCursorNotified() {
        return cursorNotified;
    }

    public void setCursorNotified(boolean cursorNotified) {
        this.cursorNotified = cursorNotified;
    }

    public int getMaxLength() {
        return maxLength;
    }

    public void setMaxLength(int maxLength) {
        this.maxLength = maxLength;
    }    

    public boolean isVisibleLine() {
        return visibleLine;
    }

    public void setVisibleLine(boolean visibleLine) {
        this.visibleLine = visibleLine;
    }

    public Color getSoftLineColor() {
        return softLineColor;
    }

    public void setSoftLineColor(Color softLineColor) {
        this.softLineColor = softLineColor;
    }

    public Color getHardLineColor() {
        return hardLineColor;
    }

    public void setHardLineColor(Color hardLineColor) {
        this.hardLineColor = hardLineColor;
    }

    public byte getFrameLineTickness() {
        return frameLineTickness;
    }

    public void setFrameLineTickness(byte frameLineTickness) {
        this.frameLineTickness = frameLineTickness;
    }

    protected void setHighLevelProperty(int property, String value) {
        
        if(Fields.VISIBLE_LINE == property){
            
            visibleLine = Util.booleanValue(value);
            
        } else if(Fields.SOFT_LINE_COLOR == property){
            
            softLineColor = GuiUtils.getColor(value);
            
        } else if(Fields.HARD_LINE_COLOR == property){
            
            hardLineColor = GuiUtils.getColor(value);
            
        } else if(Fields.FRAME_LINE_TICKNESS == property){
            
            frameLineTickness = Byte.parseByte(value);
            
        } else if(Fields.MAX_LENGTH == property){
            
            maxLength = Integer.parseInt(value);
            
        } 
    }
        
}        
